package com.ecoolex.ying.space.admin.graphql;

import static com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type.Admin;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.dubbo.config.annotation.Reference;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.alibaba.fastjson.JSON;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.modularize.api.service.storage.FileSize;
import com.ecoolex.framework.modularize.api.service.storage.MimeType;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.modularize.api.service.storage.Upload;
import com.ecoolex.framework.modularize.api.service.storage.UploadLimit;
import com.ecoolex.framework.modularize.api.service.storage.UploadSource;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.ying.space.admin.core.AdminConstant;
import com.ecoolex.ying.space.entity.EstateProjectActivityExternalGoods;
import com.ecoolex.ying.space.pojo.AddEstateProjectActivityExternalGoodsDTO;
import com.ecoolex.ying.space.pojo.EditEstateProjectActivityExternalGoodsDTO;
import com.ecoolex.ying.space.ws.EstateProjectActivityExternalGoodsService;

@GraphQL2Module(type = Admin, description = "地产项目外部热门商品管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY, key = EstateProjectActivityExternalGoodsGraphql.AUTH, name = "地产项目外部热门商品管理")
public class EstateProjectActivityExternalGoodsGraphql {

	public static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-ESTATE-PROJECT-ACTIVITY-EXTERNAL-GOODS";
	
	private static final String AUTH_ADD = AUTH + "-ADD";
	
	private static final String AUTH_EDIT = AUTH + "-EDIT";
	
	private static final String AUTH_DEL = AUTH + "-DEL";
	
	private static final String AUTH_ONLINE = AUTH + "-ONLINE";
	
	private static final String AUTH_OFFLINE = AUTH + "-OFFLINE";
	
	private static final String AUTH_GET = AUTH + "-GET";
	
	private static final String AUTH_QUERY = AUTH + "-QUERY";
	
	@Autowired
    private StorageService storageService;
	
	@Reference
	private EstateProjectActivityExternalGoodsService service;
	
	@TypeField(type = EstateProjectActivityExternalGoods.class, field = "image", description = "地产项目外部热门商品封面图片")
    public StorageInfo image(@Source EstateProjectActivityExternalGoods source) {
        return storageService.get(source.getImage());
    }
	
	@TypeField(type = EstateProjectActivityExternalGoods.class, field = "images", description = "地产项目外部热门商品轮播图片")
	public List<StorageInfo> images(@Source EstateProjectActivityExternalGoods source){
		if(Check.notNullOrTrimEmpty(source.getImages())) {
			return JSON.parseArray(source.getImages(), String.class).stream()
				.map(storageService::get)
				.filter(Check::notNull)
				.collect(Collectors.toList());
		}
		return Lists.newArrayList();
	}
	
	@TypeField(type = EstateProjectActivityExternalGoods.class,field = "canOnline",description = "能上线")
	public Boolean canOnline(@Source EstateProjectActivityExternalGoods source) {
		return Check.eq(false, source.getOnline());
	}
	
	@TypeField(type = EstateProjectActivityExternalGoods.class,field = "canOffline",description = "能下线")
	public Boolean canOffline(@Source EstateProjectActivityExternalGoods source) {
		return Check.eq(true, source.getOnline());
	}
	
	@QueryField(field = "estateProjectActivityExternalGoods" , description = "地产项目外部热门商品")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "地产项目外部热门商品")
	public EstateProjectActivityExternalGoods get(@NonNull Integer externalGoodsId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_GET);
		return service.get(externalGoodsId);
	}
	
	@QueryField(field = "estateProjectActivityExternalGoodsList" , description = "地产项目外部热门商品列表")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "地产项目外部热门商品列表")
	public Pagination<EstateProjectActivityExternalGoods> query(@NonNull Integer projectId,String name,Integer externalGoodsCategoryId,@NonNull Sorting[] sorts,@NonNull Page page,AdminMemberAuthority user) {
		user.assertAuth(AUTH_QUERY);
		return service.query(projectId,name,externalGoodsCategoryId, sorts, page);
	}
	
	@MutationField(field = "uploadEstateProjectActivityExternalGoodsImage", description = { "上传地产项目外部热门商品封面图片" })
    public StorageInfo uploadEstateProjectActivityExternalGoodsImage(@NonNull Upload request, AdminMemberAuthority user) {
        if (!user.auth(AUTH_ADD) && !user.auth(AUTH_EDIT)) {
            user.notAuthority();
        }
        UploadSource uploadSource = new UploadSource("EstateProjectActivityExternalGoods", "Image");
        UploadLimit uploadLimit = new UploadLimit();
        uploadLimit.setMaxSize(FileSize.MB.byteLength(10));
        uploadLimit.setAllowMimeType(MimeType.IMAGE.commons());

        return storageService.upload(request, uploadSource, uploadLimit);
    }
	
	@MutationField(field = "uploadEstateProjectActivityExternalGoodsImages", description = { "上传地产项目外部热门商品轮播图片" })
    public StorageInfo uploadEstateProjectActivityExternalGoodsImages(@NonNull Upload request, AdminMemberAuthority user) {
        if (!user.auth(AUTH_ADD) && !user.auth(AUTH_EDIT)) {
            user.notAuthority();
        }
        UploadSource uploadSource = new UploadSource("EstateProjectActivityExternalGoods", "Images");
        UploadLimit uploadLimit = new UploadLimit();
        uploadLimit.setMaxSize(FileSize.MB.byteLength(10));
        uploadLimit.setAllowMimeType(MimeType.IMAGE.commons());

        return storageService.upload(request, uploadSource, uploadLimit);
    }
	
	@MutationField(field = "uploadEstateProjectActivityExternalGoodsDetail", description = { "上传地产项目外部热门商品详情" })
    public StorageInfo uploadEstateProjectActivityExternalGoodsDetail(@NonNull Upload request, AdminMemberAuthority user) {
        if (!user.auth(AUTH_ADD) && !user.auth(AUTH_EDIT)) {
            user.notAuthority();
        }
        UploadSource uploadSource = new UploadSource("EstateProjectActivityExternalGoods", "Detail");
        UploadLimit uploadLimit = new UploadLimit();
        uploadLimit.setMaxSize(FileSize.MB.byteLength(10));
        uploadLimit.setAllowMimeType(MimeType.IMAGE.commons());

        return storageService.upload(request, uploadSource, uploadLimit);
    }
	
	@MutationField(field = "addEstateProjectActivityExternalGoods", description = "添加地产项目外部热门商品")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加地产项目外部热门商品")
	public EstateProjectActivityExternalGoods add(@NonNull AddEstateProjectActivityExternalGoodsDTO add,AdminMemberAuthority user) {
		user.assertAuth(AUTH_ADD);
		
		return service.add(add, user.userName());
	}
	
	@MutationField(field = "editEstateProjectActivityExternalGoods", description = "编辑地产项目外部热门商品")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑地产项目外部热门商品")
	public EstateProjectActivityExternalGoods edit(@NonNull EditEstateProjectActivityExternalGoodsDTO edit,AdminMemberAuthority user) {
		user.assertAuth(AUTH_EDIT);
		EstateProjectActivityExternalGoods old = service.get(edit.getExternalGoodsId());
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "地产项目外部热门商品不存在");
		BeanUtil.copy(edit, old);
		return service.edit(old, user.userName());
	}
	
	@MutationField(field = "delEstateProjectActivityExternalGoods", description = "删除地产项目外部热门商品")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除地产项目外部热门商品")
	public Boolean delete(@NonNull Integer externalGoodsId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_DEL);
		EstateProjectActivityExternalGoods old = service.get(externalGoodsId);
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "地产项目外部热门商品不存在");
		service.delete(old, user.userName());
		return true;
	}
	
	@MutationField(field = "onlineEstateProjectActivityExternalGoods", description = "上线地产项目外部热门商品")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ONLINE, name = "上线地产项目外部热门商品")
	public Boolean online(@NonNull Integer externalGoodsId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_ONLINE);
		EstateProjectActivityExternalGoods old = service.get(externalGoodsId);
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "地产项目外部热门商品不存在");
		service.online(old, user.userName());
		return true;
	}
	
	@MutationField(field = "offlineEstateProjectActivityExternalGoods", description = "下线地产项目外部热门商品")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_OFFLINE, name = "下线地产项目外部热门商品")
	public Boolean offline(@NonNull Integer externalGoodsId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_OFFLINE);
		EstateProjectActivityExternalGoods old = service.get(externalGoodsId);
		Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "地产项目外部热门商品不存在");
		service.offline(old, user.userName());
		return true;
	}
}
